package com.application.DataProvider;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.application.DTO.UsingFeelListDTO;

public class UsingFeelListDAO {
	Context _mcontext = null;
	public UsingFeelListDAO(Context context){
		this._mcontext = context;
	}
	public boolean isExist(int idFeelType, int idFeelChoice)
	{
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		boolean result = false;
		Cursor cur = null;
		try 
		{
			String sql = "Select count(*) from UsingFeelList where IDFeelChoice=" +  idFeelChoice + " and idFeelType="+ idFeelType;
			cur = provider.execQuery(db, sql);
			while (cur.moveToNext()) {
				int i = Integer.valueOf(cur.getString(0));
				if(i != 0)
					result = true;
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			cur.close();
			db.close();
		}
		return result;
	}
	public ArrayList<UsingFeelListDTO> getUsingFeelList_idFeelChoice(int IDFeelChoice)
	{
		ArrayList<UsingFeelListDTO> uflList = new ArrayList<UsingFeelListDTO>();
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		Cursor cur = null;
		try 
		{
			String sql = "Select * from UsingFeelList where idFeelChoice = " + IDFeelChoice;
			cur = provider.execQuery(db, sql);
			while (cur.moveToNext()) {
				UsingFeelListDTO uflDto = new UsingFeelListDTO();
				uflDto.set_idFeelChoice(cur.getInt(1));
				uflDto.set_idFeelType(cur.getInt(0));
				uflList.add(uflDto);
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			cur.close();
			db.close();
		}

		return uflList;
	}
	
	public ArrayList<UsingFeelListDTO> getUsingFeelList_idFeelType(int IDFeelType)
	{
		ArrayList<UsingFeelListDTO> uflList = new ArrayList<UsingFeelListDTO>();
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		Cursor cur = null;
		try 
		{
			String sql = "Select * from UsingFeelList where idFeelChoice = " + IDFeelType;
			cur = provider.execQuery(db, sql);
			while (cur.moveToNext()) {
				UsingFeelListDTO uflDto = new UsingFeelListDTO();
				uflDto.set_idFeelChoice(cur.getInt(1));
				uflDto.set_idFeelType(cur.getInt(0));
				uflList.add(uflDto);
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			cur.close();
			db.close();
		}

		return uflList;
	}
	
	
	public int insert(UsingFeelListDTO uflDto) {
		Log.d("PEERDROID","insert FeelChoice");
		int result = 0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try {
			ContentValues values = new ContentValues();
			values.put("IDFeelType", uflDto.get_idFeelType());
			values.put("IDFeelChoice", uflDto.get_idFeelChoice());
			
			result = provider.execInsertCommand(db, "UsingFeelList", values);
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			db.close();
		}

		return result;
	}
	
	public int insert(int idFeelType, int idFeelChoice) {
		Log.d("PEERDROID","insert FeelChoice");
		int result = 0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try {
			ContentValues values = new ContentValues();
			values.put("IDFeelType", idFeelType);
			values.put("IDFeelChoice", idFeelChoice);
			
			result = provider.execInsertCommand(db, "UsingFeelList", values);
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			db.close();
		}

		return result;
	}
	
	public int delete(int idFeelType, int idFeelChoice)
	{
		int result=0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try{	
			String whereClause="IDFeelType=? and IDFeelChoice=?";
			String[]whereArgs = {String.valueOf(idFeelType), String.valueOf(idFeelChoice)};	
			result=provider.execDeleteCommand(db, "FeelChoice", whereClause, whereArgs);
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			db.close();
		}
		return result;
	}
}
